package edu.hcmus.sow.domain;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

import org.codehaus.jackson.annotate.JsonIgnore;

/**
 */

@Entity
@NamedQueries({
      @NamedQuery(name = "findAllFiles", query = "select myFile from File myFile"),
      @NamedQuery(name = "findFileById", query = "select myFile from File myFile where myFile.id = ?1"),
      @NamedQuery(name = "findFileByName", query = "select myFile from File myFile where myFile.name = ?1"),
      @NamedQuery(name = "findFileByNameContaining", query = "select myFile from File myFile where myFile.name like ?1"),
      @NamedQuery(name = "findFileByPrimaryKey", query = "select myFile from File myFile where myFile.id = ?1") })
@Table(catalog = "sow", name = "file")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "SharedOnlineWorkspace/edu/hcmus/sow/domain", name = "File")
@XmlRootElement(namespace = "SharedOnlineWorkspace/edu/hcmus/sow/domain")
public class File implements Serializable {
   private static final long serialVersionUID = 1L;

   /**
	 */

   @Column(name = "ID", nullable = false)
   @Basic(fetch = FetchType.EAGER)
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @XmlElement
   Integer id;
   /**
	 */

   @Column(name = "NAME", length = 100, nullable = false)
   @Basic(fetch = FetchType.EAGER)
   @XmlElement
   String name;

   /**
    */
   @Column(name = "DEFAULT_ACCESS_RIGHT", length = 1)
   @Basic(fetch = FetchType.EAGER)
   @XmlElement
   String defaultAccessRight;

   /**
	 */
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumns({ @JoinColumn(name = "FOLDER_ID", referencedColumnName = "ID") })
   @XmlTransient
   Folder folder;
   /**
	 */
   @OneToMany(mappedBy = "file", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
   @XmlElement(name = "", namespace = "")
   java.util.Set<edu.hcmus.sow.domain.FileRevision> fileRevisions;
   /**
    */
   @OneToMany(mappedBy = "file", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
   @XmlElement(name = "", namespace = "")
   java.util.Set<edu.hcmus.sow.domain.FileAccessRight> fileAccessRights;

   @Column(name = "FOLDER_ID", updatable = false, insertable = false)
   private Integer folderID;

   public Integer getFolderID() {
      return folderID;
   }

   public void setFolderID(Integer folderID) {
      this.folderID = folderID;
   }

   /**
	 */
   public void setId(Integer id) {
      this.id = id;
   }

   /**
	 */
   public Integer getId() {
      return this.id;
   }

   /**
	 */
   public void setName(String name) {
      this.name = name;
   }

   /**
	 */
   public String getName() {
      return this.name;
   }

   /**
    */
   public String getDefaultAccessRight() {
      return defaultAccessRight;
   }

   /**
    */
   public void setDefaultAccessRight(String defaultAccessRight) {
      this.defaultAccessRight = defaultAccessRight;
   }

   /**
	 */
   public void setFolder(Folder folder) {
      this.folder = folder;
   }

   /**
	 */
   @JsonIgnore
   public Folder getFolder() {
      return folder;
   }

   /**
	 */
   public void setFileRevisions(Set<FileRevision> fileRevisions) {
      this.fileRevisions = fileRevisions;
   }

   /**
	 */
   @JsonIgnore
   public Set<FileRevision> getFileRevisions() {
      if (fileRevisions == null) {
         fileRevisions = new java.util.LinkedHashSet<edu.hcmus.sow.domain.FileRevision>();
      }
      return fileRevisions;
   }

   /**
    */
   public void setFileAccessRights(Set<FileAccessRight> fileAccessRights) {
      this.fileAccessRights = fileAccessRights;
   }

   /**
    */
   @JsonIgnore
   public Set<FileAccessRight> getFileAccessRights() {
      if (fileAccessRights == null) {
         fileAccessRights = new java.util.LinkedHashSet<edu.hcmus.sow.domain.FileAccessRight>();
      }
      return fileAccessRights;
   }

   /**
	 */
   public File() {
   }

   /**
    * Copies the contents of the specified bean into this bean.
    * 
    */
   public void copy(File that) {
      setId(that.getId());
      setName(that.getName());
      setDefaultAccessRight(that.getDefaultAccessRight());
      setFolder(that.getFolder());
      setFileRevisions(new java.util.LinkedHashSet<edu.hcmus.sow.domain.FileRevision>(that.getFileRevisions()));
      setFileAccessRights(new java.util.LinkedHashSet<edu.hcmus.sow.domain.FileAccessRight>(that.getFileAccessRights()));
   }

   /**
    * Returns a textual representation of a bean.
    * 
    */
   public String toString() {

      StringBuilder buffer = new StringBuilder();

      buffer.append("id=[").append(id).append("] ");
      buffer.append("name=[").append(name).append("] ");
      buffer.append("defaultAccessRight=[").append(defaultAccessRight).append("] ");

      return buffer.toString();
   }

   /**
	 */
   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
      return result;
   }

   /**
	 */
   public boolean equals(Object obj) {
      if (obj == this)
         return true;
      if (!(obj instanceof File))
         return false;
      File equalCheck = (File) obj;
      if ((id == null && equalCheck.id != null) || (id != null && equalCheck.id == null))
         return false;
      if (id != null && !id.equals(equalCheck.id))
         return false;
      return true;
   }
}
